﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace ByteUtilHelpV2
{
    public enum ByteArrayToHexType
    {
        Loop,
        BitConverter,
        ConvertAll
    }
    public class ByteToolV2
    {
        public static string ToHexString(byte[] bytes, ByteArrayToHexType type)
        {
            try
            {
                if (bytes == null)
                    throw new ArgumentNullException("bytes");
                string _toHexString = string.Empty;
                switch (type)
                {
                    case ByteArrayToHexType.Loop:
                        _toHexString = ToHexStringByLoop(bytes);
                        break;
                    case ByteArrayToHexType.BitConverter:
                        _toHexString = ToHexStringByBitConverter(bytes);
                        break;
                    case ByteArrayToHexType.ConvertAll:
                        _toHexString = ToHexStringByConvertAll(bytes);
                        break;
                }
                return _toHexString;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ToHexString Exception:{0}", ex.Message.Trim()));
            }
        }
        private static string ToHexStringByConvertAll(byte[] bytes)
        {
            try
            {
                return String.Concat(Array.ConvertAll(bytes, x => x.ToString("X2")));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static string ToHexStringByBitConverter(byte[] bytes)
        {
            try
            {
                string _toHexString = BitConverter.ToString(bytes);
                return _toHexString.Replace("-", "");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string ToHexStringWithBlank(byte[] bytes)
        {
            try
            {
                string _toHexString = BitConverter.ToString(bytes);
                return _toHexString.Replace("-", " ");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static string ToHexStringByLoop(byte[] bytes)
        {
            try
            {
                StringBuilder _hex = new StringBuilder(bytes.Length * 2);
                foreach (byte b in bytes)
                    _hex.AppendFormat("{0:X2}", b);
                return _hex.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static byte[] ConvertHexString(String hex)
        {
            try
            {
                hex = hex.Replace(" ", "");
                int _hexLen = hex.Length;
                if (_hexLen % 2 != 0)
                    throw new ArgumentException(String.Format("hex:{0}", hex));
                byte[] _bytes = new byte[_hexLen / 2];
                for (int i = 0; i < _hexLen; i += 2)
                    _bytes[i / 2] = byte.Parse(hex.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);//Convert.ToByte(hex.Substring(i, 2), 16);
                return _bytes;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("ConvertHexString Exception:{0}", ex.Message.Trim()));
            }
        }
      
    }

}
